
import { sandbox } from '@/libs/util'

export default {
  props: {
    mode: {
      type: String
    },
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    visible: {
      get () {
        return this.dialogVisible
      },
      set (val) {
        this.$emit('update:dialogVisible', val)
      }
    }
  },
  data (vm) {
    let opt = {
        btnLoading: false,
        rules: {},
        form: sandbox({})
      },
      tar = {}

    for (let key in opt) {
      if (!(key in vm)) {
        Object.assign(tar, { [key]: opt[key] })
      }
    }

    return tar
  },
  methods: {
    AddRow () {
      return Promise.resolve()
    },
    UpdateRow () {
      return Promise.resolve()
    },
    GetDetail (row) {
      return this.$api
        .API_WEBDEALERGROUP_GETDEALERGROUP_POST({
          body: { Id: row.Id }
        })
        .then(res => res.Entity)
    },
    SetData (row) {
      return this.GetDetail(row).then(res => {
        Object.assign(this.form, res)
        this.visible = true
      })
    },
    // dialogClose
    dialogClose () {
      this.btnLoading = false
      this.$refs.groupForm && this.$refs.groupForm.resetFields()
      this.form.reset()
    },
    // dialogOpen
    dialogOpen () {
      //   this.$emit('dialogOpen', this.handelMode)
    },
    submitForm () {
      this.$refs.groupForm.validate(valid => {
       
        if (!valid) {
          this.$message({
            type: 'warning',
            message: '当前有必填项未填写'
          })
        }
        if (valid) {
          this.btnLoading = true
          ;(this.mode === 'add' ? this.AddRow() : this.UpdateRow())
            .then(() => {
              this.$emit('complete')
              this.visible = false
            })
            .finally(() => {
              this.btnLoading = false
            })
        }
      })
    }
  }
}
